//导入defineStore函数
import {defineStore} from 'pinia'
import api from '@/api'
import IUserState from '@/types/IUserState'
import {RouteRecordRaw} from 'vue-router'
import {getViews} from '@/utils/routedync'
//通过该函数创建store对象
const useUserStore=defineStore('users',{
    state:():IUserState=>{
        return{
            token:'',
            permissionList:[]
        }
    },
    getters:{
        //返回首页路由对象
        getHomeRoute(state:IUserState){
            let routeObj:RouteRecordRaw={
                path:'/',
                component:()=>import('@/views/Home.vue'),
                children:[]
            }
            let ary:Array<RouteRecordRaw>=[]
            state.permissionList.forEach((item:any)=>{
                if(item.children){
                    item.children.forEach((subItem:any)=>{                        
                        ary.push({
                            path:subItem.path,
                            meta:{title:subItem.title,path:subItem.path},
                            component:getViews(`../views${subItem.path}.vue`)
                        })
                    })
                } 
            })
            routeObj.children=ary
            return routeObj
        }
    },
    actions:{
        setToken(token:string){
            this.token=token
        },
        async getAuthMenuAsync(){
           const result=await api.users.getAuthMenus()
           this.permissionList=result.data.data
           console.log('权限',result.data.data);
        }
    },
    persist:{
        enabled:true,
        strategies:[
            {
                key:'users',
                storage:localStorage
            }
        ]
    }
})
export default useUserStore